package com.zl.study.mybatis.demo;


import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class Main2{

  public static void main(String[] args) {
    int num;
    Scanner scanner = new Scanner(System.in);

    while(true) {
      try {
        num = scanner.nextInt();
      } catch (Exception e) {
        System.out.println("ERROR");
        return;
      }

      if (num < 0) {
        System.out.println("ERROR");
        return;
      }

      if (check(num)) {
        System.out.println(num);
        return;
      }


      int dist = 1;

      Set<Integer> set = new TreeSet<>();

      while (num - dist >= 0 || num + dist <= Integer.MAX_VALUE) {

        if (num - dist >= 0) {
          if (check(num - dist)) set.add(num - dist);
        }
        if (num + dist <= Integer.MAX_VALUE) {
          if (check(num + dist)) set.add(num + dist);
        }

        if (!set.isEmpty()) {
          break;
        }
        dist++;
      }

      for (Integer n : set) {
        System.out.println(n);
      }
    }

  }

  // 判断一个数是不是对称数
  public static boolean check(int num) {
    String str = String.valueOf(num);
    int len = str.length();

    int end = len-1;
    int start = 0;
    while(end >= start) {
      if(str.charAt(end) == str.charAt(start)) {
        end--;
        start++;
      } else {
        break;
      }
    }

    return end <= start;
  }
}
